package cn.wolfcode.luowowo.website.controller;


import cn.wolfcode.luowowo.acticle.service.ISceneryService;
import cn.wolfcode.luowowo.comment.domain.SceneryComment;
import cn.wolfcode.luowowo.comment.domain.SceneryReply;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.SceneryCommentQuery;
import cn.wolfcode.luowowo.comment.query.SceneryReplyQuery;
import cn.wolfcode.luowowo.comment.service.ISceneryCommentService;
import cn.wolfcode.luowowo.comment.service.ISceneryReplyService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Controller
@RequestMapping("scenery")
public class SceneryController {

    @Value("${file.path}")
    private String filePath;

    @Reference
    private ISceneryCommentService sceneryCommentService;

    @Reference
    private ISceneryService sceneryService;

    @Reference
    private ISceneryReplyService sceneryReplyService;

    @RequestMapping("scenery")
    public String scenery(Model model, Long id) {

        return "scenery/detail";
    }


    @RequestMapping("detail")
    public String detail(Model model, Long id) {

        model.addAttribute("detail", sceneryService.get(id));

        //关联评论数据
        SceneryCommentQuery ctqo = new SceneryCommentQuery();
        ctqo.setSceneryId(id);
        ctqo.setPageSize(Integer.MAX_VALUE);
        Page<SceneryComment> page = sceneryCommentService.findCommentBySceneryId(ctqo);

        List<SceneryComment> commentList = page.getContent();

        SceneryReplyQuery crqo = null;

        for (SceneryComment comment : commentList) {
            crqo = new SceneryReplyQuery();
            crqo.setCommentId(comment.getId());
            crqo.setPageSize(Integer.MAX_VALUE);
            List<SceneryReply> replyList = sceneryReplyService.findReplyByCommentId(crqo).getContent();
            comment.setSceneryReplies(replyList);
        }

        model.addAttribute("list", commentList);

        return "scenery/detail";
    }

    /*@RequestMapping("reply")
    public String reply(Model model, Long commentId) {

        //关联回复数据
        SceneryReplyQuery crqo = new SceneryReplyQuery();
        crqo.setCommentId(commentId);
        crqo.setPageSize(Integer.MAX_VALUE);
        Page<SceneryReply> page = sceneryReplyService.findReplyByCommentId(crqo);

        model.addAttribute("reply",page.getContent());

        return "scenery/detail";
    }*/

    //添加景点点评
    @RequireLogin
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(Model model, SceneryComment comment, @UserParam UserInfo userInfo) {

        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setUserLevel(userInfo.getLevel());

        sceneryCommentService.save(comment);

        return new AjaxResult();
    }

    //添加点评回复
    @RequireLogin
    @RequestMapping("/replyAdd")
    @ResponseBody
    public Object replyAdd(Model model, SceneryReply reply, @UserParam UserInfo userInfo) {

        reply.setUserId(userInfo.getId());
        reply.setUsername(userInfo.getNickname());
        reply.setHeadUrl(userInfo.getHeadImgUrl());
        reply.setUserLevel(userInfo.getLevel());

        sceneryReplyService.save(reply);

        return new AjaxResult();
    }

    //上传图片
    @RequestMapping("/updateImg")
    @ResponseBody
    public Object upload(MultipartFile file) {
        String url = "";
        try {
            url = UploadUtil.upload(file, filePath);
        } catch (Exception e) {
            return new AjaxResult(0, null);
        }

        return new AjaxResult(1, url);

    }

    //评论点赞thumbUp
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    @RequireLogin
    public Object thumbup(String commentId, Long fromid, @UserParam UserInfo userInfo) {
        //commentid是评论的id, form是点赞者id
        boolean ret = sceneryCommentService.thumbUp(commentId, fromid);
        return new AjaxResult(ret, "").addData(sceneryCommentService.getComment(commentId).getThumbupnum());
    }
}
